package com.github.edu.repair.web.controller;

import com.github.admin.edu.assembly.common.entity.JsonArray;
import com.github.admin.edu.assembly.common.util.JsonUtils;
import com.github.admin.edu.assembly.page.entity.Pager;
import com.github.edu.repair.api.entity.TBxBxsq;
import com.github.edu.repair.api.entity.TBxFwfl;
import com.github.edu.repair.api.entity.TBxGzms;
import com.github.edu.repair.web.service.UserRepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Create by IntelliJ IDEA
 * 用户：王建
 * 日期：2018-8-22
 */
@Controller
@RequestMapping("/uc/apply")
public class UserRepairWebController extends BaseController{

    @Autowired
    private UserRepairService service;

    @GetMapping("/index")
    public String index(ModelMap map, HttpServletRequest request){
        return super.index(request,"index/apply/index");
    }

    /**
     * 用户填报
     * @param map
     * @param request
     * @return
     */
    @GetMapping("/repair/add")
    public String userRepairAdd(ModelMap map,HttpServletRequest request){
        service.userRepair(map,getUserID());
        map.put("url","/uc/apply/repair/save");
        map.put("button",true);
        map.put("msg","");
        map.put("stepId","");
        return super.index(request,"index/apply/index");
    }

    /**
     * 第一步，用户保存操作
     * @return
     */
    @PostMapping("/repair/save")
    public String userSave(ModelMap map, TBxBxsq tBxBxsq,HttpServletRequest request,Integer stepId){
        service.saveRepair(map,tBxBxsq,stepId);
        map.put("button",true);
        map.put("msg","保存成功！");
        map.put("url","/uc/apply/repair/save");
        return super.index(request,"index/apply/index");
    }

    /**
     * 执行指定步骤
     * @param map
     * @param tBxBxsq
     * @param request
     * @param stepId 当前步骤id
     * @return
     */
    @PostMapping("/repair/{stepId}/save")
    public String userStep(ModelMap map,TBxBxsq tBxBxsq,HttpServletRequest request,@PathVariable("stepId")Integer stepId){
        boolean info=service.saveStep(map,stepId,tBxBxsq,getUserID());
        map.put("button",true);
        map.put("msg","提交失败！");
        if(info){
            map.put("button",false);
            map.put("msg","提交成功！");
        }
        map.put("url","/uc/apply/repair/save");
        return super.index(request,"index/apply/index");
    }

    /**
     * ajax根据服务分类，获取服务类型
     * @param id
     * @return
     */
    @GetMapping("/ajax/fwfl")
    @ResponseBody
    public String getFwfl(@RequestParam("id") Integer id){
        List<TBxFwfl> list=service.getAllFwfl(id);
        JsonArray<TBxFwfl> jsonArray=new JsonArray<>();
        jsonArray.setDataList(list);
        return JsonUtils.toJson(jsonArray);
    }

    /**
     * 故障描述
     * @return
     */
    @RequestMapping("/gznr/index")
    public String getGznr(ModelMap map, TBxGzms tBxGzms, Pager pager,
                          HttpServletRequest request){
        service.getAllGzms(map,tBxGzms,pager);
        return super.index(request,"index/apply/alert");
    }

    @GetMapping("/ajax/gznr")
    @ResponseBody
    public String getAjaxGznr(TBxGzms tBxGzms){
        return service.getAllJsonGzms(tBxGzms);
    }


}
